package com.example.controller;

import com.example.cac409club.response.RestResult;
import com.example.dto.RoleDTO;
import com.example.entity.RoleDO;
import com.example.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   角色管理控制器
 * </p>
 * @author 34448
 */
@Api(tags = "角色管理")
@RequestMapping("/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     *
     * @param roleDTO 角色信息
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增角色")
    public RestResult<Boolean> addRole(@Valid @RequestBody RoleDTO roleDTO) {
        Boolean result = roleService.addRole(roleDTO);
        return new RestResult<>("000000", "调用成功", result);
    }

    /**
     * 删除角色
     *
     * @param roleId 角色ID
     * @return
     */
    @DeleteMapping("/delete/{roleId}")
    @ApiOperation("删除角色")
    public RestResult<Boolean> deleteRole(@PathVariable Integer roleId) {
        Boolean result = roleService.deleteRole(roleId);
        return new RestResult<>("000000", "调用成功", result);
    }

    /**
     * 编辑角色
     *
     * @param roleDTO 角色信息
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation("编辑角色")
    public RestResult<Boolean> editRole(@Valid @RequestBody RoleDTO roleDTO) {
        Boolean result = roleService.editRole(roleDTO);
        return new RestResult<>("000000", "调用成功", result);
    }

    /**
     * 查询角色
     *
     * @param roleName 角色名称
     * @return
     */
    @GetMapping("/query")
    @ApiOperation("查询角色")
    public RestResult<List<RoleDO>> queryRoles(@RequestParam(required = false) String roleName) {
        List<RoleDO> roles = roleService.queryRoles(roleName);
        return new RestResult<>("000000", "调用成功", roles);
    }
}